﻿package frEngine.postProcess
{
    import __AS3__.vec.*;
    import baseEngine.system.*;
    import flash.display3D.*;
    import flash.geom.*;
    import frEngine.core.*;
    import frEngine.primitives.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import frEngine.shader.filters.vertexFilters.*;

    public class PostPass extends Object
    {
        private var _clearColor:Vector3D;
        private var _viewPort:Rectangle;
        protected var _testQuad:FrQuad;
        protected var _texFilter:TextureFilter;
        public var output:PostTextureBuffer = null;
        public var isWriteToScene:Boolean = false;
        public var uvScaleValueFS0:Vector.<Number>;
        public var uvOffsetValueFS0:Vector.<Number>;
        public var uvScaleValueFS1:Vector.<Number>;
        public var uvOffsetValueFS1:Vector.<Number>;
        private static const defaultUv:Vector.<Number> = PostPass.Vector.<Number>([1, 1, 0, 0]);

        public function PostPass(param1:String = "none", param2:Number = 0, param3:Number = 0, param4:Number = 0)
        {
            this._clearColor = new Vector3D();
            this._texFilter = new TextureFilter(null);
            this._clearColor.setTo(param2, param3, param4);
            this._testQuad = new FrQuad(param1, 0, 0, 1, 1, true, null, null);
            this._testQuad.setShaderBase(new TransformFilter(), this._texFilter);
            this._testQuad.setMateiralBlendMode(EBlendType.BLEND_NONE);
            this._testQuad.material.materialParams.depthWrite = false;
            this._testQuad.material.materialParams.colorKillvalue = 0;
            this._testQuad.material.materialParams.depthCompare = Context3DCompareMode.ALWAYS;
            this._testQuad.material.materialParams.setBlendMode(EBlendType.BLEND_NONE);
            this._testQuad.addedToScene(Global3D.scene);
            return;
        }// end function

        public function setFs0(param1:PostTextureBuffer) : void
        {
            this._texFilter.texture = param1.texture3d;
            return;
        }// end function

        protected function openOffsetAdnScaleFilter(param1:Boolean) : void
        {
            if (param1)
            {
                this._testQuad.material.materialParams.uvValue2 = PostProcess.instance.uvValue;
            }
            else
            {
                this._testQuad.material.materialParams.uvValue2 = defaultUv;
            }
            return;
        }// end function

        public function setFs1(param1:PostTextureBuffer) : void
        {
            var _loc_3:FilterBase = null;
            var _loc_2:* = new TextureFsFilter(param1.texture3d, 1);
            if (this.materialParam.hasFilter(_loc_2.type))
            {
                _loc_3 = this.materialParam.getFilterByTypeId(_loc_2.type);
                this.materialParam.removeFilte(_loc_3);
                this.materialParam.addFilte(_loc_2);
            }
            else
            {
                this.materialParam.addFilte(_loc_2);
            }
            return;
        }// end function

        public function setFs2(param1:PostTextureBuffer) : void
        {
            var _loc_3:FilterBase = null;
            var _loc_2:* = new TextureFsFilter(param1.texture3d, 2);
            if (this.materialParam.hasFilter(_loc_2.type))
            {
                _loc_3 = this.materialParam.getFilterByTypeId(_loc_2.type);
                this.materialParam.removeFilte(_loc_3);
                this.materialParam.addFilte(_loc_2);
            }
            else
            {
                this.materialParam.addFilte(_loc_2);
            }
            return;
        }// end function

        public function setFs3(param1:PostTextureBuffer) : void
        {
            var _loc_3:FilterBase = null;
            var _loc_2:* = new TextureFsFilter(param1.texture3d, 3);
            if (this.materialParam.hasFilter(_loc_2.type))
            {
                _loc_3 = this.materialParam.getFilterByTypeId(_loc_2.type);
                this.materialParam.removeFilte(_loc_3);
                this.materialParam.addFilte(_loc_2);
            }
            else
            {
                this.materialParam.addFilte(_loc_2);
            }
            return;
        }// end function

        public function get materialParam() : MaterialParams
        {
            return this._testQuad.material.materialParams;
        }// end function

        public function get material() : ShaderBase
        {
            return this._testQuad.material;
        }// end function

        public function setOutput(param1:PostTextureBuffer) : void
        {
            if (param1)
            {
                this.output = param1;
                this.isWriteToScene = false;
            }
            else
            {
                this.isWriteToScene = true;
                this.output = null;
            }
            this.ViewPortResize(this._viewPort);
            return;
        }// end function

        public function ViewPortResize(param1:Rectangle) : void
        {
            this._viewPort = param1;
            if (this.isWriteToScene)
            {
                this.openOffsetAdnScaleFilter(true);
            }
            else
            {
                this.openOffsetAdnScaleFilter(false);
            }
            return;
        }// end function

        public function drawGrid(param1:Context3D) : void
        {
            if (!param1)
            {
                return;
            }
            if (this.isWriteToScene || !this.output)
            {
                param1.setScissorRectangle(null);
                param1.setRenderToBackBuffer();
                param1.clear();
                this._testQuad.draw(false, null);
            }
            else
            {
                param1.setScissorRectangle(null);
                param1.setRenderToTexture(this.output.buffer);
                param1.clear(this._clearColor.x, this._clearColor.y, this._clearColor.z, this._clearColor.w, 0, 0, Context3DClearMask.COLOR);
                this._testQuad.draw(false, null);
            }
            return;
        }// end function

    }
}
